package com.gsdse.controller;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gsdse.pojo.ReturnData;
import com.gsdse.pojo.User;
import com.gsdse.pojo.apply;
import com.gsdse.service.ApplyService;
import com.gsdse.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ApplyService applyService;


    //修改用户信息
    @RequestMapping( "/updateUser")
    public ReturnData updateAspiration(@RequestBody User user) {
        ReturnData res = new ReturnData();
        Integer id = user.getUserId();
        res.setData(user);
        if (userService.selectUserByUserId(id) != null) {

            Boolean aBoolean = userService.updateUserAndAvatarById(user);
            if (aBoolean) {
                res.setCode(0);
                res.setMsg("修改成功");
                return res;
            } else {
                res.setCode(1);
                res.setMsg("修改失败");
                return res;
            }
        } else {
            res.setCode(2);
            res.setMsg("用户id不存在");
            return res;
        }
    }



    //删除用户

    //根据用户表id查找用户
    @RequestMapping("/selectUser")
    public ReturnData selectAspiration(String userId) {
        ReturnData res = new ReturnData();
        if (StringUtils.isEmpty(userId)) {
            res.setCode(2);
            res.setMsg("参数为空");
            return res;
        }
        if("undefined".equals(userId)){
            res.setCode(1);
            res.setMsg("查询错误，结果为空");
            return res;
        }
        User select=userService.selectUserByUserId(Integer.parseInt(userId));
        if (select == null) {
            res.setCode(1);
            res.setMsg("查询错误，结果为空");
            return res;
        } else {
            res.setCode(0);
            res.setMsg("查询成功");
            res.setData(select);
            return res;
        }

    }

    //添加用户信息
    @RequestMapping("/addUser")
    public ReturnData addAspiration(User user) {
        //前端传来的只有applyid，但是userId和applyId一样，直接赋值
        System.out.println(user.getUserId());
        ReturnData res = new ReturnData();

        user.setUserId(user.getApplyId());

        //顺便在apply表中插入一条数据
        apply apply = new apply();
        apply.setApplyId(user.getApplyId());
        apply.setUserId(user.getApplyId());

        Boolean add = userService.InsertUser(user);
        applyService.insertApply(apply);

        res.setData(user);
        if (add) {
            res.setCode(0);
            res.setMsg("添加成功");
            return res;
        } else {
            res.setCode(1);
            res.setMsg("添加失败");
            return res;
        }
    }

    //根据真实姓名和电话号码查询用户
    @RequestMapping("/selectUserByRealNameAndPhone")
    public ReturnData selectUserByRealNameAndPhone(String userName,  String phoneNumber) {
        ReturnData res = new ReturnData();
        if (userName == null || phoneNumber == null) {
            res.setCode(2);
            res.setMsg("参数为空");
        } else {
            res.setData(userName);
            User user = userService.selectUserByReallyNameAndPhoneNumber(userName, phoneNumber);
            if (user == null) {
                res.setCode(1);
                res.setMsg("查询错误，结果为空");
            } else {
                user.setPhoneNumber("***********");
                res.setCode(0);
                res.setMsg("查询成功");
                res.setData(user);

            }
        }
        return res;
    }



    //根据真实姓名和邮箱查询用户
    @RequestMapping("/selectUserByRealNameAndEmail")
    public ReturnData selectUserByRealNameAndEmail(String userName,  String email) {
        ReturnData res = new ReturnData();
        if (userName == null || email == null) {
            res.setCode(2);
            res.setMsg("参数为空");
        } else {
            res.setData(userName);
            User user = userService.selectUserByReallyNameAndEmail(userName, email);
            if (user == null) {
                res.setCode(1);
                res.setMsg("查询错误，结果为空");
            } else {
                res.setCode(0);
                res.setMsg("查询成功");
                res.setData(user);

            }
        }
        return res;
    }


    //查询用户最后通过了哪个
    @PostMapping("/selectUserByLastPass")
    public ReturnData selectUserByLastPass(String UserId) {
        ReturnData res = new ReturnData();
        if (StringUtils.isNotEmpty(UserId)) {
            User user = userService.selectUserByUserId(Integer.parseInt(UserId));
            if (user == null) {
                res.setCode(1);
                res.setMsg("查询错误，用户为空");
                return res;
            } else {
                Integer applyId = user.getApplyId();
                if (applyId != null)
                {
                    apply apply = applyService.selectApplyByApplyId(applyId);
                    String firstStatus = apply.getFirstStatus();
                    String secondStatus = apply.getSecondStatus();
                    if ("1".equals(firstStatus) && "1".equals(secondStatus)) {
                        res.setCode(100);
                        res.setMsg("恭喜你被两个部门选中");
                        res.setData(apply.getFirstWilling() + "," + apply.getSecondWilling());
                    } else if ("0".equals(firstStatus) && "1".equals(secondStatus)) {
                        res.setMsg("恭喜你第二志愿通过");
                        res.setData(apply.getSecondWilling());
                    } else if ("1".equals(firstStatus) && "0".equals(secondStatus)) {
                        res.setMsg("恭喜你第一志愿通过");
                        res.setData(apply.getFirstWilling());
                    } else if ("0".equals(firstStatus) && "0".equals(secondStatus)) {
                        res.setMsg("抱歉，都没有被选中");
                    } else if(firstStatus==null || secondStatus==null){
                        res.setMsg("部门还在审核，给您带来不便了！");
                    }
                    res.setCode(0);
                } else {
                    res.setCode(3);
                    res.setMsg("该用户没有志愿数据！");
                }

            }

        }
        return res;
    }

    //上传用户头像
    @PostMapping("/uploadAvatar")
    public ReturnData uploadAvatar(@RequestParam("file") MultipartFile file) throws ClientException, IOException {

        if (file.isEmpty()) {
            ReturnData returnData = new ReturnData();
            returnData.setCode(1);
            returnData.setMsg("请上传文件！");
            return  returnData;
        }

        return userService.uploadAvatar(file);
    }

    @GetMapping("/checkHaveEdit")
    public ReturnData checkHaveEdit(@RequestParam("userId") Integer userId) {
        return userService.checkHaveEdit(userId);
    }


}









